<?php

if(!defined('IN_SYS')) exit('Access Denied');

class ads_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$pg = new page("SELECT * from ads where yuming_id=0 order by id asc");
		$list = $pg->get_list(20);
		$page = $pg->get_page();
		require self::tpl('ads');
	}

	public function add(){
		if(IS_POST){
			$time = time();
			$v = gp("abc,title,content,status", false);
			if (empty($v['abc'])) {
				self::json( array('status' => 0, 'errMsg' => '广告标记不能为空！') );
			}
			db::query("insert into ads (yuming_id,abc,title,content,addtime,status) 
			values (0,'$v[abc]','$v[title]', '$v[content]','$time','$v[status]') ");
			//dump(db::sqls());
			self::json( array('status' => 1, 'msg' => '添加成功', 'redirect' => '?m=ads') );
		}else{
			require self::tpl("ads_add");
		}
	}

	public function subadd(){
		if(IS_POST){
			$time = time();
			$v = gp("abc,ym_id,content,status", false);
			if (empty($v['abc'])) {
				self::json( array('status' => 0, 'errMsg' => '广告标记不能为空！') );
			}
			if (empty($v['ym_id'])) {
				self::json( array('status' => 0, 'errMsg' => '请选择所属站点！') );
			}
			db::query("insert into ads (yuming_id,abc,content,addtime,status) 
			values ('$v[ym_id]','$v[abc]', '$v[content]','$time','$v[status]') ");
			//dump(db::sqls());
			$pid = gp("pid");
			self::json( array('status' => 1, 'msg' => '添加成功', 'redirect' => '?m=ads&a=edit&id='.$pid) );
		}else{
			$abc = gp('abc');
			$r = $hadadd = array();
			$res = db::select("SELECT * from ads where abc='$abc'");
			foreach ($res as $v) {
				if ($v['yuming_id']==0) {
					$r = $v;
				} else {
					$hadadd[] = $v['yuming_id'];
				}
			}
			if (!$r) {
				self::redirect("?m=ads", "广告不存在");
			}

			if (count($hadadd)==count($this->allyuming)) {
				self::redirect('?m=ads&a=edit&id='.$r['id'], "广告内容已滿");
			}
			require self::tpl("ads_subadd");
		}
	}

	public function edit() {
		$id = gp('id');
		if(IS_POST) {
			$v = gp("title,content,status", false);
			db::query("update ads set title='$v[title]', content='$v[content]' ,status='$v[status]'
		 where id='$id' ");
			self::json( array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=ads') );
		} else {
			$r = db::find("select *  from ads  where yuming_id=0 and id='$id' ");
			if (!$r) {
				self::redirect("?m=ads", "广告不存在");
			}
			$abc = $r['abc'];
			$pg = new page("SELECT * from ads where yuming_id>0 and abc='$abc' order by id asc");
			$list = $pg->get_list(10);
			$page = $pg->get_page();
			require self::tpl("ads_edit");
		}
	}

	public function subedit() {
		$id = gp('id');
		if(IS_POST) {
			$v = gp("ym_id,content,status", false);
			db::query("update ads set yuming_id='$v[ym_id]', content='$v[content]' ,status='$v[status]'
		 where id='$id' ");
			$pid = gp("pid");
			self::json( array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=ads&a=edit&id='.$pid) );
		} else {
			$r = db::find("select *  from ads  where yuming_id>0 and id='$id' ");
			if (!$r) {
				self::redirect("?m=ads", "广告内容不存在");
			}
			$abc = $r['abc'];
			$pads = $hadadd = array();
			$res = db::select("SELECT * from ads where abc='$abc'");
			foreach ($res as $v) {
				if ($v['yuming_id']==0) {
					$pads = $v;
				}
				if ($v['yuming_id']==$r['yuming_id']) {
					continue;
				}
				$hadadd[] = $v['yuming_id'];
			}
			require self::tpl("ads_subedit");
		}
	}

	public function del() {
		$id = gp('id');
		$backurl = empty($_SERVER['HTTP_REFERER']) ? "?m=ads" : $_SERVER['HTTP_REFERER'];
		db::query("delete from ads where id='$id' ");
		self::redirect($backurl, "删除成功");
	}

}


